package com.web.position.entity;

import java.math.BigDecimal;
import java.time.LocalDate;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.web.transaction.entity.Transaction;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;
@ToString
@ApiModel
@Data
public class Position {

	public static String POSITION_STATUS_NORMAL = "NOR";
	public static String POSITION_STATUS_COMPLETE = "DON";

	@ApiModelProperty("positionNo")
	@Excel(name = "positionNo")
	private String positionNo;

	@ApiModelProperty("dealNo")
	@Excel(name = "dealNo")
	private String dealNo;

	@DateTimeFormat(pattern = "yyyy-MM-dd")
	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
	@ApiModelProperty("tradedate")
	@Excel(name = "tradeDate")
	private LocalDate tradeDate;

	@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
	@ApiModelProperty("valueDate")
	private LocalDate valueDate;

	@ApiModelProperty("excde")
	private String excde;

	@ApiModelProperty("currency")
	@Excel(name = "currency")
	private String currency;

	@ApiModelProperty("amount")
	@Excel(name = "amount")
	private BigDecimal amount;

	@ApiModelProperty("bankCostRate")
	private BigDecimal bankCostRate;

	@ApiModelProperty("hkdEquAmt")
	@Excel(name = "hkdEquAmt",width = 15)
	private BigDecimal hkdEquAmt;

	@ApiModelProperty("buySellOption")
	private String buySellOption;

	@ApiModelProperty("accumulatedNo")
	private String accumulatedNo;

	@ApiModelProperty("status")
	@Excel(name = "status",width = 15)
	private String status;

	@ApiModelProperty("counterparty")
	private String counterparty;

	@ApiModelProperty("bankCode")
	private String bankCode;

	public Position genBuyPosition(Transaction t, BigDecimal hkdEquAmt) {
		Position p = new Position();
		p.setTradeDate(t.getTradeDate());
		p.setValueDate(t.getValueDate());
		p.setDealNo(t.getDealNo());
		p.setBuySellOption("B");
		p.setExcde(t.getExcde());
		p.setCurrency(t.getWithdrawalCcy());
		p.setAmount(t.getWithdrawalAmt());
		p.setHkdEquAmt(hkdEquAmt);
		p.setBankCostRate(t.getBankCostRate());
		p.setCounterparty(t.getCounterparty());
		p.setBankCode(t.getBankCode());
		p.setStatus(POSITION_STATUS_NORMAL);
		return p;
	}

	public Position genSellPosition(Transaction t, BigDecimal hkdEquAmt) {
		Position p = new Position();
		p.setTradeDate(t.getTradeDate());
		p.setValueDate(t.getValueDate());
		p.setDealNo(t.getDealNo());
		p.setBuySellOption("S");
		p.setExcde(t.getExcde());
		p.setCurrency(t.getDepositeCcy());
		p.setAmount(t.getDepositedAmt());
		p.setHkdEquAmt(hkdEquAmt);
		p.setBankCostRate(t.getBankCostRate());
		p.setCounterparty(t.getCounterparty());
		p.setBankCode(t.getBankCode());
		p.setStatus(POSITION_STATUS_NORMAL);
		return p;
	}

	public boolean isBankBuy() {
		return Transaction.TX_BUY.equals(getBuySellOption());
	}

	public boolean isBankSell() {
		return Transaction.TX_SELL.equals(getBuySellOption());
	}

	public String groupKey() {
		return getTradeDate().format(Transaction.yyyyMMdd)+"#"+getCurrency()+"#"+getBuySellOption();
	}
}
